package com.situ.struts.interceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.Interceptor;

import java.util.Map;

public class LoginInterceptor implements Interceptor {

    @Override
    public void init() {

    }

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        System.out.println("LoginInterceptor.intercept 开始");
        ActionContext actionContext = invocation.getInvocationContext();
        Map<String, Object> sessionMap = actionContext.getSession();
        // 获得当前要执行的方法名
        ActionProxy proxy = invocation.getProxy();
        String method = proxy.getMethod();
        System.out.println("method:" + method);
        if ("login".equals(method) == false) {
            Object user = sessionMap.get("user");
            if (user == null) {
                return "input";
            }
        }

        String result = invocation.invoke();
        System.out.println("LoginInterceptor.intercept 结束");
        return result;
    }

    @Override
    public void destroy() {

    }
}
